package com.zyzg.utils;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    public static final Long nowTimeStamp()
    {
        return new Date().getTime()/1000;
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 获取当前小时整点时间戳
     * @return
     */
    public static Long getCurrentHour() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HH");
        Long currentHourTime=0L;
        try {
            currentHourTime = sdf.parse(sdf.format(new Date())).getTime()/1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return currentHourTime;
    }
    /**
     * 获取当前年月
     * @return
     */
    public static String getCurrentYearMonth(Long currentTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        return sdf.format(new Date(Long.valueOf(currentTime+"000")));
    }
    public static String getCurrentYearMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        return sdf.format(new Date());
    }

    public static String getCurrentYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return sdf.format(new Date());
    }
    /**
     * 获取下个月
     * @return
     */
    public static String getPreMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(cal.MONTH, 1);
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
        String preMonth = dft.format(cal.getTime());
        return preMonth;
    }
    /**
     * 获取上个月
     * @return
     */
    public static String getLastMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(cal.MONTH, -1);
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
        String lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }

    /**
     * 获取表名日期
     * @param time
     * @param rankType
     * @return
     */
    public static String getFormatTime(Long time,String rankType) {
        if (rankType.equals("day")){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
            return sdf.format(time*1000);
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月");
            return sdf.format(time*1000);
        }
    }

    /**
     * 获取指定的时间格式
     * @param pattern
     * @param time
     * @return
     */
    public static String getFormatByPattern(String pattern,Long time) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(time*1000);
    }

    public static String getHms(Long time) {
        //秒数
        if (time < 60) return time % 60 + "秒";
        long second = time % 60;
		if (time < 3600) return Math.round(time / 60) + "分钟" + (second == 0 ? "" : second + "秒");
        long hour = Math.round(time / 3600);
        long minute = Math.round((time - (hour * 3600)) / 60);
		return hour + "小时" + (minute == 0 ? "" : minute + "分钟") + (second == 0 ? "" : second + "秒");

    }

    public static Long[] getDayStAndEt(String day) {
        Long[] longs = new Long[2];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            long time = sdf.parse(day).getTime()/1000;
            longs[0]=time;
            longs[1]=time+24*60*60;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return longs;
    }


//    public static void main(String[] args) {
//        System.out.println(getTimeYearMonth(1588749288L));
//    }

    /**
     * 获取当前时间与今天0点间隔的秒数
     * */
    public static Long getDayStartsToNow(Long time) {
        //秒数
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);
        return time - cal.getTime().getTime()/1000;
    }

    /**
     * 获取当天0点时间秒数
     * */
    public static Long getCurrentDayStart() {
        //秒数
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime().getTime();
    }

    /**
     * 获取当天0点时间秒数
     * */
    public static Date getDateByCurrentDayStart() {
        //秒数
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public static String getCurrentLastMonth(int i) {
        Calendar cal = Calendar.getInstance();
        cal.add(cal.MONTH, -i);
        SimpleDateFormat dft = new SimpleDateFormat("yyyyMM");
        String lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }
    /**
     * 获取指定时间的年月
     * */
    public static String getTimeYearMonth(Long getsTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String format = sdf.format(getsTime * 1000);
        return format;
    }

    /**
     * 获取指定时间的时间戳
     * */
    public static Date getDataByFormat(String str,String format) {
        try {
            return new SimpleDateFormat(format).parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 获取指定时间的时间戳
     * */
    public static Long getTimestampByFormat(String str,String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Long time = 0L;
        try {
            time = sdf.parse(str).getTime();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return time;

    }

    public static Date getCurrentDay() {
        return new Date();
    }

    public static Long getAgoDayStart() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH),
                0, 0, 0);
        cal.add(Calendar.DAY_OF_MONTH, -1);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime().getTime();
    }

    public static final Date handleGpsTime(final String format, final String ts)
    {
        try
        {
            return new Date(new SimpleDateFormat(format).parse(ts).getTime());
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }
}
